package com.woniuxy.stage3demo.service.chenyxuanService.serviceImpl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.mapper.chenyuxuanMapper.DeptMapper;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.service.chenyxuanService.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class DeptServiceImpl implements IDeptService {
    @Resource
    private DeptMapper deptMapper;

    /**
     * 根据ID查询部门信息
     *
     * @param id
     * @return
     */
    @Override
    public DeptBean findOneById(int id) {
        return deptMapper.findOneById(id);
    }

    /**
     * 分页查询部门信息
     *
     * @param pageRequest
     * @param deptBean
     * @return
     */
    @Override
    public PageInfo<DeptBean> findAllDept(PageRequest pageRequest, DeptBean deptBean) {
        return PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getLimit()).
                doSelectPageInfo(() -> deptMapper.findAllDept(pageRequest, deptBean));
    }


    /**
     * 添加部门
     *
     * @param deptBean
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int addDept(DeptBean deptBean) {
        return deptMapper.addDept(deptBean);
    }

    /**
     * 修改部门
     *
     * @param deptBean
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public int updateDept(DeptBean deptBean) {
        return  deptMapper.updateDept(deptBean);
    }


    /**
     * 删除部门
     *
     * @param id
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    @Override
    public void deleteDept(int id) {
        deptMapper.deleteDept(id);
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<DeptBean> findAll() {
        return deptMapper.findAll();
    }

}
